package com.mcxx.modules.miniSalvation.family.service.write;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.miniSalvation.family.param.ExpireFamilyParam;
import com.mcxx.modules.miniSalvation.order.dao.write.OrderCertWriteDao;
import com.mcxx.modules.miniSalvation.order.dao.write.SuspendFamilyWriteDao;
import com.mcxx.modules.miniSalvation.order.entity.OrderEntity;
import com.mcxx.modules.miniSalvation.order.entity.SuspendFamilyEntity;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service("suspendFamilyWriteService")
@TaTransactional
public class SuspendFamilyWriteService extends BaseService {
    @Autowired
    private SuspendFamilyWriteDao suspendFamilyWriteDao;
    @Autowired
    private OrderCertWriteDao orderCertWriteDao;


    public int saveSuspendFamily(ExpireFamilyParam param, UserAccountVo userAccountVo, OrderEntity orderEntity,String primaryKey) {
        SuspendFamilyEntity entity = new SuspendFamilyEntity();
        entity.setId(primaryKey);
        entity.setStartMonth(param.getStopMonth());
        entity.setRemark(param.getRemark());
        entity.setOrgId(userAccountVo.getOrgId());
        entity.setAreaCode(orderEntity.getAreaCode());
        entity.setFamilyId(orderEntity.getFamilyId());
        entity.setMemberId(orderEntity.getMemberId());
        entity.setOrderId(orderEntity.getOrderId());
        entity.setOperatorDate(DateUtil.getTimestamp());
        entity.setOperator(userAccountVo.getUserId());
        if(Constant.UserLevel.COUNTY.equals(userAccountVo.getAreaLevel())){

            entity.setState(Constant.SuspendState.SUSPEND);
        }else if(Constant.UserLevel.STREET.equals(userAccountVo.getAreaLevel())){
            entity.setState(Constant.SuspendState.S_SUSPEND);
        }

        entity.setBusinessType(param.getBusinessType());

        if(StringUtils.isNotBlank(param.getOrderCertIds())){
            String [] certArr = param.getOrderCertIds().split(",");
            List<String> orderCertIdList = Stream.of(certArr).collect(Collectors.toList());
            int rst = orderCertWriteDao.updateById(orderCertIdList, entity.getOrderId(), "7", entity.getId());
            if(rst <= 0){
                throw new BizException("-1", "暂停申请的证明材料操作 异常");
            }
        }

        return suspendFamilyWriteDao.insertIgnoreNull(entity);
    }


    public int deleteById(String id) {
        return  suspendFamilyWriteDao.deleteById(id);
    }

    public int updateIgnoreNull(SuspendFamilyEntity entity) {
        return suspendFamilyWriteDao.updateIgnoreNull(entity);
    }
}
